<?php

namespace App\Admin\Controllers\Common;

use App\Admin\Repositories\Common\CommonGridLatitude;
use App\Admin\Controllers\CommonController;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Layout\Column;
use App\Admin\Renderable\PostTable;
use App\Admin\Renderable\ReportDetail;
use Dcat\Admin\Widgets\Modal;
use App\Admin\Controllers\Common\SelectorArray;
use App\Admin\Controllers\Common\DetailConfigArray;
use App\Admin\Controllers\Common\ColumnArray;
use Dcat\Admin\Widgets\Table;

/**
 * 看板呈现类
 */
class CommonLatitude extends AdminController
{
	protected $title = '';

	public function __construct()
	{
		$this->title = date('m月d日', strtotime('-7 days')).'~'.date('m月d日', time());
        if (request('between')) {
            $this->title = date('m月d日', strtotime(request('between')['start'])).'~'.date('m月d日', strtotime(request('between')['end']));
        }
        if (request('date')) {
            $this->title = date('m月d日', strtotime(request('date')));
        }
	}

	public function channelInvite(Content $content)
	{
		return $content->header('渠道看板')->description($this->title)->body($this->commonGridLatitude('channelInvite'));
	}

	

	public function commonGridLatitude($board)
	{
		$grid = Grid::make(new CommonGridLatitude($board), function (Grid $grid) use ($board) {
			// 组合表头
			$combile_header = ColumnArray::$latitudeArr[$board]['combile_header'] ?? [];
			foreach ($combile_header as $v) {
				$style = $v['style'] ?? 'text-align:center;';
				$name = $v['name'];
				if ($name == '当前筛选周期') {
					$name .= '('.$this->title.')';
				}
				$grid->combine($name, $v['column'])->style($style);
			}

			$grid->column('name', '维度')->display(function () {
				return $this['data']['name'];
			})->sortable();
			$column_arr = ColumnArray::$latitudeArr[$board]['column'];
			$help_arr = ColumnArray::$latitudeArr[$board]['help'] ?? [];
			$hide_arr = ColumnArray::$latitudeArr[$board]['hide'] ?? [];
			$no_percent = ColumnArray::$latitudeArr[$board]['no_percent'] ?? [];
			// 特殊选择改变表头
			$select = request('_selector') ?? [];
			$select_header_map = ColumnArray::$latitudeArr[$board]['select_header_map'] ?? [];
			$select_hide_arr = ColumnArray::$latitudeArr[$board]['select_header_hide'] ?? [];
			$column_select_map = [];
			// dd($select, $select_header_map);
			foreach ($select as $k => $v) {
				// 特殊选择改变表头
				foreach ($select_header_map as $sk => $map_arr) {
					if ($k == $sk) {
						foreach ($map_arr as $mk => $mv) {
							if ($mk == $v) {
								$column_select_map = $mv;
							}
						}
					}
				}
				// 特殊选择隐藏表头
				foreach ($select_hide_arr as $shk => $sharr) {
					if ($k == $shk) {
						foreach ($sharr as $shkk => $shvv) {
							if ($shkk == $v) {
								$hide_arr = array_merge($hide_arr, $shvv);
							}
						}
					}
				}
			}
			// dd($column_select_map);
			foreach ($column_arr as $ck => $cv) {
				if (in_array($ck, $hide_arr)) {
					continue;
				}
				$help_text = $help_arr[$ck] ?? '暂无';
				// 特殊选择改变表头
				$cv = $column_select_map[$ck] ?? $cv;
				$grid->column($ck, $cv)->display(function () use ($board, $ck, $cv, $no_percent) {
					$total = $this['data'][$ck] ?? 0;
					//如果含有斜杠的则是率添加百分号
					if (strpos($ck, '/') !== false && !in_array($ck,$no_percent)) {
						$total = $total . '%';
					}
					return Modal::make()->lg()->title('明细')->body(ReportDetail::makeCustomerDetail($this['data'][$ck.'_detail'] ?? [], ColumnArray::$latitudeArr[$board]['detail']))->button('<span>'.$total.'</span>');
				})->sortable()->help($help_text)->style('text-align:center;');
			}
			
			$grid->column('expand', '展开')->expand(function () use ($board, $column_arr) {
				if (!isset($this['lat'])) {
					return '-';
				}
                $table = new Table();
                $data = [];
                foreach ($this['lat'] as $k => $l) {
                	// dd($this['lat']);
                	$temp = [$l['data']['name']];
                	foreach ($column_arr as $ck => $cv) {
                		// dd($l['data']);
                		$total = $l['data'][$ck] ?? 0;
                		array_push($temp, Modal::make()->lg()->title('明细')->body(ReportDetail::makeCustomerDetail($l['data'][$ck.'_detail'] ?? [], ColumnArray::$latitudeArr[$board]['detail']))->button('<span>'.$total.'</span>'));
                		
                	}
                	$data[] = $temp;
                }
		        $table = $table->make(array_merge(['维度'], $column_arr), $data);
		        return $table;
            });

            $grid->disableActions();
            $grid->disableRowSelector();
            $grid->disablePerPages();
            if (!in_array('date', ColumnArray::$latitudeArr[$board]['select_hide'] ?? [])) {
            	// 日期区间
				$grid->filter(function (Grid\Filter $filter) {
	                $filter->panel();
	                $filter->equal('date', '单个日期')->date();
	                $filter->between('between', '日期区间')->date();
	            });
            }
         	$beginDate = request('between')['start'] ?? date('Y-m-d', strtotime('-6 days'));
	        $endDate = request('between')['end'] ?? date('Y-m-d', time());
	        if (request('date')) {
	            $beginDate = request('date');
	            $endDate = request('date');
	        }
	        //额外筛选
	        $config = [];
	        if (isset(ColumnArray::$latitudeArr[$board]['other_select'])) {
	            foreach (ColumnArray::$latitudeArr[$board]['other_select'] as $k => $v) {
	                if (isset(request('_selector')[$v['key']]) && request('_selector')[$v['key']]) {
	                    $config[$v['key']] = request('_selector')[$v['key']];
	                }
	            }
	        }
	        $lat1 = request('_selector')['latitude1'] ?? array_keys(ColumnArray::$latitudeArr[$board]['select'])[0];
        	$lat2 = request('_selector')['latitude2'] ?? array_keys(ColumnArray::$latitudeArr[$board]['select'])[1];
        	
			// 维度筛选
			$grid->selector(function (Grid\Tools\Selector $selector) use ($board) {
				if (isset(ColumnArray::$latitudeArr[$board]['other_select'])) {
					foreach (ColumnArray::$latitudeArr[$board]['other_select'] as $k => $v) {
						$selector->select($v['key'], $v['name'], $v['value']);
					}
				}
				$selector->select('latitude1', '维度1', ColumnArray::$latitudeArr[$board]['select']);
				$selector->select('latitude2', '维度2', ColumnArray::$latitudeArr[$board]['select']);
			});
		});
		return $grid;
	}
}